var Menus = function(){
	var tree;
	var grid;
	var fs,win;

	var buildTree = function(){
		var root = new Ext.tree.AsyncTreeNode({
			id:'0',
			text:'菜单列表'
		});
		tree = new Ext.tree.TreePanel({
			loader:new Ext.tree.TreeLoader({dataUrl:'./?ctrl=menus&action=getMain'}),
			root:root,
			renderTo:'tree',
			border:false
		});
		tree.on('click',function(node){
			if(node.parentNode)
			grid.store.load({params:{pid:node.parentNode.id}});
			else
			grid.store.load({params:{pid:0}});
		});
		tree.on('beforeload',function(node){
			tree.loader.dataUrl = './?ctrl=menus&action=get&pid='+node.id;
		});
		root.expand();
		root.select();
	};

	var buildGrid = function(){
		var editcombo=new Ext.form.ComboBox({
			typeAhead: true,
			triggerAction: 'all',
			transform:'pid',
			lazyRender:true,
			hiddenName:'pid',
			valueField:'pid',
			mode:'local',
			listClass: 'x-combo-list-small'
		});
		editcombo.on('change',function(obj, newValue, oldValue){
			;
		});
		var pidrend=function(value ,metadata ,record ,rowIndex ,colIndex ,store ){
			return rootMenu[value];
		};
		grid = new Ext.grid.EditorGridPanel({
			store:new Ext.data.Store({
				proxy:new Ext.data.HttpProxy({url:'./?ctrl=menus&action=get'}),
				reader:new Ext.data.JsonReader({},['id','url','title','pid','comment'])
			}),
			columns:[
			new Ext.grid.RowNumberer({header :'id'}),
			{header:'URL链接',width:300,sortable:true,dataIndex:'url',editor:new Ext.form.TextField()},
			{header:'菜单标题',width:150,sortable:true,dataIndex:'title',editor:new Ext.form.TextField()},
			{header:'父菜单',width:150,sortable:true,dataIndex:'pid',renderer:function(val){return rootMenu[val];},
			editor:editcombo},
			{header:'说明',width:300,sortable:true,dataIndex:'comment',editor:new Ext.form.TextField()}
			],
			viewConfig:{
				forceFit:true
			},
			renderTo:'grid',
			border:false
		});
		grid.on('afteredit',function(e){
			Ext.Ajax.request({
				url:'./?ctrl=menus&action=update',
				params:{
				'id':e.record.data.id,
				'menu[url]':e.record.data.url,
				'menu[title]':e.record.data.title,
				'menu[pid]':e.record.data.pid,
				'menu[comment]':e.record.data.comment
				},
				method:'POST',
				success:function(){
					e.record.commit(false);
					var node = tree.getSelectionModel().getSelectedNode();
					if(node.parentNode){
						node=node.parentNode;
					}
					node=node.findChild('id',e.record.data.id);
					if(node)
					node.setText(e.record.data.title);
				}
			});
		});
		grid.store.load({params:{id:1}});
	};

	var buildLayout = function(){
		var toolbar = new Ext.Panel({
			border:false,
			tbar:[{
				text:'新增',
				cls:'x-btn-text-icon',
				icon:RES_ROOT_PATH+'images/add.gif',
				handler:createType
			},'-',{
				text:'删除',
				cls:'x-btn-text-icon',
				icon:RES_ROOT_PATH+'images/delete.gif',
				//enableToggle:true,
				//pressed:true,
				handler:function(){
					Ext.Msg.confirm('提示', '是否确认删除？', function(btn){
						if (btn == 'yes'){
							var node = tree.getSelectionModel().getSelectedNode();
							if(node){
								Ext.Ajax.request({
									url:'./?ctrl=menus&action=delete',
									params:{
										id:node.id
									},
									method:'POST',
									success:function(response){
										node.remove();
										node.parentNode.getUI().removeClass('x-tree-node-leaf');
										node.parentNode.getUI().addClass('x-tree-node-expanded');
										node.parentNode.expand();
										grid.parentNode.store.reload();
									}
								});
							}
						}
					},this);
				}
			}]
		});
		var layout = new Ext.Viewport({
			layout:'border',
			defaults:{border:false},
			items:[{
				region:'north',
				title:'菜单管理',
				height:52,
				items:toolbar
			},{
				region:'west',
				width:180,
				split:true,
				autoScroll:true,
				items:tree
			},{
				region:'center',
				layout:'fit',
				title:'编辑区',
				items:grid
			}]
		});
	};

	var createType = function(){
		var node = tree.getSelectionModel().getSelectedNode();
		if(node.parentNode){
			Ext.Ajax.request({
				url:'./?ctrl=menus&action=create',
				params:{
					pid:node.parentNode.id
				},
				method:'POST',
				success:function(response){
					//alert(response.responseText);
					var resp = Ext.util.JSON.decode(response.responseText)
					node.parentNode.appendChild(new Ext.tree.TreeNode({
						id:resp.id,
						url:resp.url,
						pid:resp.pid,
						title:resp.title,
						text:resp.title,
						comment:resp.comment,
						leaf:true
					}));
					node.parentNode.getUI().removeClass('x-tree-node-leaf');
					node.parentNode.getUI().addClass('x-tree-node-expanded');
					node.parentNode.expand();
					grid.store.reload();
				}
			});
		}
	};

	return{
		init:function(){
			buildTree();
			buildGrid();
			buildLayout();
		}
	}
}();
Ext.onReady(Menus.init,Menus);
